package com.demo.template.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class CustomController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/conf")
    public String test(){
        return restTemplate.getForEntity("http://nacos-discovery1/nacos-discovery/discovery/test", String.class).getBody();
    }

    @GetMapping("/normal/conf")
    public String test1(){

        //获取服务
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-discovery");

        //根据服务拼接路径
        String url = String.format("http://%s:%s/nacos-discovery/discovery/test",serviceInstance.getHost(),serviceInstance.getPort());

        return restTemplate.getForEntity(url, String.class).getBody();
    }
}
